/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.cms.rpc.service.impl;

import java.util.Date;

import com.lambkit.common.service.LambkitModelServiceImpl;
import com.lambkit.core.aop.AopKit;
import com.lambkit.db.sql.column.Example;
import com.lambkit.module.cms.common.CmsConsts.RefType;
import com.lambkit.module.cms.rpc.api.CmsFavoriteService;
import com.lambkit.module.cms.rpc.model.CmsFavorite;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-02-23
 * @version 1.0
 * @since 1.0
 */
public class CmsFavoriteServiceImpl extends LambkitModelServiceImpl<CmsFavorite> implements CmsFavoriteService {
	
	private CmsFavorite DAO = null;
	
	public CmsFavorite dao() {
		if(DAO==null) {
			DAO = AopKit.singleton(CmsFavorite.class);
		}
		return DAO;
	}

	@Override
	public boolean judge(Long userId, Long refid, RefType type) {
		// TODO Auto-generated method stub
		Example example = CmsFavorite.sql().andUserIdEqualTo(userId)
				.andRefIdEqualTo(refid).andRefTypeEqualTo(type.value()).example();
		CmsFavorite f = findFirst(example);
		return f == null ? false : true;
	}

	@Override
	public boolean fork(Long userId, Long refid, RefType type, Long categoryId, String tags) {
		// TODO Auto-generated method stub
		Example example = CmsFavorite.sql().andUserIdEqualTo(userId)
				.andRefIdEqualTo(refid).andRefTypeEqualTo(type.value()).example();
		CmsFavorite cf = findFirst(example);
		if(cf!=null) {
			cf.setRefCategory(categoryId);
			cf.setRefTag(tags);
			return cf.update();
		} else {
			CmsFavorite f = new CmsFavorite();
			f.setUserId(userId);
			f.setRefId(refid);
			f.setRefType(type.value());
			f.setRefCategory(categoryId);
			f.setRefTag(tags);
			f.setCtime(new Date());
			return f.save();
		}
	}
	
	@Override
	public boolean forkNote(Long userId, Long refid, RefType type, Long categoryId, String note) {
		// TODO Auto-generated method stub
		Example example = CmsFavorite.sql().andUserIdEqualTo(userId)
				.andRefIdEqualTo(refid).andRefTypeEqualTo(type.value()).example();
		CmsFavorite cf = findFirst(example);
		if(cf!=null) {
			cf.setRefCategory(categoryId);
			cf.setNote(note);
			return cf.update();
		} else {
			CmsFavorite f = new CmsFavorite();
			f.setUserId(userId);
			f.setRefId(refid);
			f.setRefType(type.value());
			f.setRefCategory(categoryId);
			f.setNote(note);
			f.setCtime(new Date());
			return f.save();
		}
	}

	@Override
	public boolean unfork(Long userId, Long refid, RefType type) {
		// TODO Auto-generated method stub
		Example example = CmsFavorite.sql().andUserIdEqualTo(userId)
				.andRefIdEqualTo(refid).andRefTypeEqualTo(type.value()).example();
		CmsFavorite f = findFirst(example);
		if(f!=null) return f.delete();
		return true;
	}

	@Override
	public boolean unfork(Long forkId) {
		// TODO Auto-generated method stub
		CmsFavorite f = findById(forkId);
		if(f!=null) return f.delete();
		return true;
	}

	@Override
	public CmsFavorite get(Long userId, Long refid, RefType type) {
		// TODO Auto-generated method stub
		Example example = CmsFavorite.sql().andUserIdEqualTo(userId)
				.andRefIdEqualTo(refid).andRefTypeEqualTo(type.value()).example();
		return findFirst(example);
	}

	
}
